package com.fenghuajueli.lib_data.database.manager;

import android.content.Context;

import com.fenghuajueli.lib_data.database.DaoManager;
import com.fenghuajueli.lib_data.entity.db.CollectionDbEntity;
import com.fenghuajueli.lib_data.entity.db.CollectionDbEntityDao;

import java.util.List;

/**
 * 草稿箱记录表
 */
public class KaDianCollectionManager {
    private DaoManager manager;

    public KaDianCollectionManager(Context context) {
        manager = DaoManager.getInstance();
        manager.init(context);
    }


    public boolean insertData(CollectionDbEntity collectionDbEntity) {
        return manager.getDaoSession().insertOrReplace(collectionDbEntity) != -1;
    }

    /**
     * 插入多条记录，需要开辟新的线程
     *
     * @param CollectionDbEntityList
     * @return flag
     **/
    public boolean insertList(final List<CollectionDbEntity> CollectionDbEntityList) {
        boolean flag = false;
        try {
            manager.getDaoSession().runInTx(() -> {
                for (CollectionDbEntity CollectionDbEntity : CollectionDbEntityList) {
                    manager.getDaoSession().insertOrReplace(CollectionDbEntity);
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }


    public boolean updateData(CollectionDbEntity CollectionDbEntity) {
        boolean flag = false;
        try {
            manager.getDaoSession().update(CollectionDbEntity);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * @param CollectionDbEntity
     * @return flag
     */
    public boolean delete(CollectionDbEntity CollectionDbEntity) {
        boolean flag = false;
        try {
            manager.getDaoSession().delete(CollectionDbEntity);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }


    public void deleteAll(Class cls) {
        manager.getDaoSession().deleteAll(cls);
    }

    /**
     * 返回多行记录 * * @return
     */
    public List<CollectionDbEntity> listAll() {
        return manager.getDaoSession().getCollectionDbEntityDao().queryBuilder().orderDesc(CollectionDbEntityDao.Properties.ModifyTime).list();
    }

    /**
     * 按照主键返回单行记录 * * @param key * @return
     */
    public CollectionDbEntity getOneData(long id) {
        return manager.getDaoSession().load(CollectionDbEntity.class, id);
    }

    /**
     * 查询短语表中所有数据
     */
    public List<CollectionDbEntity> queryAll() {
        return manager.getDaoSession().loadAll(CollectionDbEntity.class);
    }


}
